﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class DynamesAI:AI
    {
        private Point2D e,d;
        private int phaseChange = 90;
        private List<Ability> specialAbilityList;

        public void DoAction(Character c, GameController gc)
        {
            if (specialAbilityList == null)
                CreateAbilities(c);

            Character enemy = (gc.GameStages.Peek().FindPlayer() as Character);
            if (enemy == null) return;

            e.x = Geometry.CenterPoint(c.ObjectSprite).X;
            e.y = Geometry.CenterPoint(c.ObjectSprite).Y;

            d.x = Geometry.CenterPoint(enemy.ObjectSprite).X;
            d.y = Geometry.CenterPoint(enemy.ObjectSprite).Y;

            Vector v = Geometry.VectorFromPoints(e, d);

            if (!c.GlobalLockout)
            {
                if (c.State == GlobalConst.SpriteState.normalEast && e.x - d.x > 0)
                {
                    
                    c.State = GlobalConst.SpriteState.normalWest;
                    GlobalConst.Utils.ActivateAnimationRow(c.ObjectSprite, c.offSetSpriteRow(), GlobalConst.DefaultColEnd, GlobalConst.DefaultFPC);
                }
                else if (c.State == GlobalConst.SpriteState.normalWest && e.x - d.x < 0)
                {
                    c.State = GlobalConst.SpriteState.normalEast;
                    GlobalConst.Utils.ActivateAnimationRow(c.ObjectSprite, c.offSetSpriteRow(), GlobalConst.DefaultColEnd, GlobalConst.DefaultFPC);
                }
            }

            if (((float)c.health / (float)c.maxHealth) * 100f < phaseChange)
            {
                RemoveAction(c);
                AddAction(c);
                phaseChange -= 10;
            }            

            foreach (Ability a in c.CurrentSet.SetOfAbilities)
            {
                if (a.Name == "Balance" || a.BossAbility || Geometry.VectorMagnitude(v) < 200 && (d.y - e.y < 40 || d.y - e.y > -40) && !c.GlobalLockout && a.Name == "Slash"
                    || Geometry.VectorMagnitude(v) > 200 && !c.GlobalLockout && a.Name == "Shoot"  || a.IsActive)
                    a.Update(gc);
            }
        }

        public void Draw(Character c)
        {
            foreach (Ability a in c.CurrentSet.SetOfAbilities)
                if (a.IsActive)
                    a.Draw();
        }

        private void RemoveAction(Character c)
        {
            foreach (Ability a in c.CurrentSet.SetOfAbilities)
                if (a.BossAbility)
                {
                    c.CurrentSet.SetOfAbilities.Remove(a);
                    return;
                }
        }

        private void AddAction(Character c)
        {

            if (((float)c.health / (float)c.maxHealth) * 100f < 90 && ((float)c.health / (float)c.maxHealth) * 100f > 81)
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[4]);
            else if (((float)c.health / (float)c.maxHealth) * 100f < 80 && ((float)c.health / (float)c.maxHealth) * 100f > 71)
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[1]);
            else if (((float)c.health / (float)c.maxHealth) * 100f < 70 && ((float)c.health / (float)c.maxHealth) * 100f > 61)
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[2]);
            else if (((float)c.health / (float)c.maxHealth) * 100f < 60 && ((float)c.health / (float)c.maxHealth) * 100f > 51)
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[4]);
            else if (((float)c.health / (float)c.maxHealth) * 100f < 50 && ((float)c.health / (float)c.maxHealth) * 100f > 41)
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[3]);
            else if (((float)c.health / (float)c.maxHealth) * 100f < 40 && ((float)c.health / (float)c.maxHealth) * 100f > 31)
            {
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[0]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[2]);
            }
            else if (((float)c.health / (float)c.maxHealth) * 100f < 30 && ((float)c.health / (float)c.maxHealth) * 100f > 21)
            {
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[3]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[4]);
            }
            else if (((float)c.health / (float)c.maxHealth) * 100f < 20 && ((float)c.health / (float)c.maxHealth) * 100f > 11)
            {
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[3]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[1]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[2]);
            }
            else if (((float)c.health / (float)c.maxHealth) * 100f < 10 && ((float)c.health / (float)c.maxHealth) * 100f > 1)
            {
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[0]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[1]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[2]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[3]);
                c.CurrentSet.SetOfAbilities.Add(specialAbilityList[4]);
            }
        }

        private void CreateAbilities(Character c)
        {
            specialAbilityList = new List<Ability>();
            Ability newAbility;
            newAbility = new Ability(c, KeyCode.vk_w, "Void Zone", 100000);
            newAbility.AbilityAction = new VoidZones(c, true, 3, 4, GlobalConst.DefaultFPC, 0, true);
            newAbility.AbilityAction.Immune = true;
            newAbility.BossAbility = true;
            specialAbilityList.Add(newAbility);

            newAbility = new Ability(c, KeyCode.vk_d, "Gravity", 20000);
            newAbility.AbilityAction = new Gravity(c, true, 3, 4, GlobalConst.DefaultFPC, 0, 300, true, 10000);
            newAbility.AbilityAction.Immune = true;
            newAbility.BossAbility = true;
            specialAbilityList.Add(newAbility);

            newAbility = new Ability(c, KeyCode.vk_e, "Explode", 20000);
            newAbility.AbilityAction = new Explode(c, true, 3, 4, GlobalConst.DefaultFPC, 0, 60000, 7000, 1000, true);
            newAbility.AbilityAction.Immune = true;
            newAbility.BossAbility = true;
            specialAbilityList.Add(newAbility);

            newAbility = new Ability(c, KeyCode.vk_a, "Touhou One", 20000);
            newAbility.AbilityAction = new Touhou1(c, true, 4, 4, GlobalConst.DefaultFPC, 0, 100, GlobalConst.ScreenWidth / 2, GlobalConst.ScreenHeight / 2, 8
                , 5000, 10000, true);
            newAbility.AbilityAction.Immune = true;
            newAbility.BossAbility = true;
            specialAbilityList.Add(newAbility);

            newAbility = new Ability(c, KeyCode.vk_s, "Touhou Tsu", 40000);
            newAbility.AbilityAction = new Touhou2(c, true, 4, 4, GlobalConst.DefaultFPC, 0, 100, GlobalConst.ScreenWidth / 2, GlobalConst.ScreenHeight / 2, 5
                , true);
            newAbility.AbilityAction.Immune = true;
            newAbility.BossAbility = true;
            specialAbilityList.Add(newAbility);
        }
    }
}
